using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using Prodma.DataAccess.Models;
using System.Web.Security;
using Prodma.DataAccess.Authorization;
using Prodma.DataAccess.Helper.Enumerators;
using Prodma.DataAccess.DataService.ListService;
using Prodma.DataAccess.Database;
using System.Configuration;
using System.Data;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.DataService;

namespace Prodma.Panel.Controllers
{
    public class SiparisController : Controller
    {
        #region Repository Setters

        UserService service = new UserService();
        public SiparisController()
        {
        }

        #endregion


        #region Index
        public ActionResult Index()
        {
            int page = 0;
            if (Request.QueryString["page"] != "" && Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }

            if (Request.QueryString["find"] != "" && Request.QueryString["find"] != null)
            {
                Dictionary<string, string> find = new Dictionary<string, string>();
                find.Add("name", Request.QueryString["find"]);
                ViewData["totalRowCount"] = service.GetTotalRowCount(find);
                return View(service.GetList(page, find));
            }
            else
            {
                ViewData["TotalRowCount"] = service.GetTotalRowCount();
                return View(service.GetList(page));
            }
        }
        #endregion


        #region New
        public ActionResult New()
        {
            //ViewData["menuAdminParentList"] = MenuAdminList.GetParentItems();
            ViewData["roleList"] = RoleList.GetList();
            ViewData["stateList"] = StateList.GetList(StateType.General);

            System.Web.HttpContext.Current.Session["userImage"] = null;

            return View();
        }

        public ActionResult New(FormCollection form)
        {
            //ViewData["menuAdminParentList"] = MenuAdminList.GetParentItems();
            ViewData["roleList"] = RoleList.GetList();
            ViewData["stateList"] = StateList.GetList(StateType.General);

            tb_User user = new tb_User();
            user.UserName = Request.Form["userName"];
            user.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(Request.Form["password"], "md5").ToLower();
            user.FirstName = Request.Form["firstName"];
            user.LastName = Request.Form["lastName"];
            user.Mail = Request.Form["mail"];
            string userImage = ConfigurationManager.AppSettings["userBlankImage"].ToString();
            if (System.Web.HttpContext.Current.Session["userImage"] != null)
            {
                userImage = System.Web.HttpContext.Current.Session["userImage"].ToString();
            }
            //user.Photo = userImage;
            user.tb_RoleReference.EntityKey = new EntityKey("DataContext.tb_Role", "ID", Convert.ToInt32(Request.Form["ddlRole"]));
            user.LK_DURUMReference.EntityKey = new EntityKey("DataContext.LK_DURUM", "ID", Convert.ToInt32(Request.Form["ddlState"]));

            service.New(user);
            return View();
        }
        #endregion


        #region Edit
        public ActionResult Edit()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            UserVm user = service.Edit(id);

            //ViewData["roleList"] = RoleList.GetList(user.Role.ID);
            //ViewData["stateList"] = StateList.GetList(StateType.General, user.State.Id);

            //System.Web.HttpContext.Current.Session["userImage"] = user.Photo;

            return View(user);
        }

        public ActionResult Edit(FormCollection form)
        {
            tb_User user = new tb_User();
            user.FirstName = Request.Form["firstName"];
            user.LastName = Request.Form["lastName"];
            user.Mail = Request.Form["mail"];

            string userImage = ConfigurationManager.AppSettings["userBlankImage"].ToString();
            if (System.Web.HttpContext.Current.Session["userImage"] != null)
            {
                userImage = System.Web.HttpContext.Current.Session["userImage"].ToString();
            }
            //user.Photo = userImage;
            user.tb_RoleReference.EntityKey = new EntityKey("DataContext.tb_Role", "ID", Convert.ToInt32(Request.Form["ddlRole"]));
            user.LK_DURUMReference.EntityKey = new EntityKey("DataContext.LK_DURUM", "ID", Convert.ToInt32(Request.Form["ddlState"]));

            int id = Convert.ToInt32(Request.Form["id"]);
            int page = 0;
            if (Request.QueryString["page"] != "" && Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"]);
            }
            service.Update(id, user);

            return RedirectToAction("edit/?id=" + id + "&page=" + page);
        }
        #endregion


        #region Delete
        public ActionResult Delete()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string deleteId = id.Substring(3, id.Length - 3);
                service.Delete(Convert.ToInt32(deleteId));
            }

            return RedirectToAction("index");
        }
        #endregion


        #region Set Active && Passive
        public ActionResult SetActive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                service.SetActive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        public ActionResult SetPassive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                service.SetPassive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        #endregion

 
        #region Change UserName and Password
        public ActionResult ChangeUserNameAndPassword()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);

            tb_User user = new tb_User();
            user.UserName = Request.QueryString["userName"];
            user.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(Request.QueryString["password"], "md5").ToLower();

            service.ChangeUserNameAndPassword(id, user);

            return RedirectToAction("edit/?id=" + Request.QueryString["id"] + "&page=" + Request.QueryString["page"]);
        }
        #endregion

    }
}
